import TreeCesiumData from "./layer_tree_data";
import Environment from "../../utils/environment";
import LayerManager from "@/plugins/mev/layer/LayerManager";
import axios from "axios"
import * as Cesium from "cesium";

class BillboardData extends TreeCesiumData {
    constructor(data, viewer) {
        super(data, viewer);
        this._data = data;
        this._viewer = viewer;
        this._dataObject = null;
        this._dataObjectLabel = null;
        this._rect = null;
        this._primitiveIndex = 0;
    }

    show() {
        let billboardCollection = new Cesium.BillboardCollection();
        let options = this._data.options;
        const color = options.color !== undefined ? Cesium.Color.fromCssColorString(options.color) : Cesium.Color.fromBytes(255, 255, 255, 255);
        const pixelOffset = options.pixelOffset !== undefined ? new Cesium.Cartesian2(options.pixelOffset[0], options.pixelOffset[1]) : new Cesium.Cartesian2(0, 0);
        const eyeOffset = options.eyeOffset !== undefined ? new Cesium.Cartesian2(options.eyeOffset[0], options.eyeOffset[1]) : undefined;

        let labelCollection = new Cesium.BillboardCollection();
        const labelField = this._data.labelField !== undefined ? this._data.labelField : "name";
        const labelPixelOffset = options.labelPixelOffset !== undefined ? new Cesium.Cartesian2(pixelOffset.x, pixelOffset.y + options.labelPixelOffset) : undefined;
        // For canvas context
        let labelFont = options.labelFont;
        if (labelFont === undefined) {
            const fontStyle = options.labelFontStyle !== undefined ? options.labelFontStyle : "normal";
            const fontSize = options.labelFontSize !== undefined ? options.labelFontSize : 12;
            const fontFamily = options.labelFontFamily !== undefined ? options.labelFontFamily : "MicroSoft YaHei";
            labelFont = fontStyle + " " + fontSize + "px " + fontFamily
        }

        const labelBackgroundColorCanvas = options.labelBackgroundColor !== undefined ? options.labelBackgroundColor : "#00000000";
        const labelColorCanvas = options.labelColor !== undefined ? options.labelColor : "#000000";
        var canvastext = document.createElement('canvas');
        canvastext.width = 200;
        canvastext.height = 100;
        var ctx = canvastext.getContext('2d');

        let that = this;
        axios.get(this._data.basePath + this._data.path + "?datatype=json").then(function (response) {
                let json = response.data;
                let west = 180.0;
                let east = -180.0;
                let north = -90.0;
                let south = 90.0;
                json.features.forEach(function (feature) {

                    billboardCollection.add({
                        position: Cesium.Cartesian3.fromDegrees(
                            feature.geometry.coordinates[0],
                            feature.geometry.coordinates[1],
                            100
                        ),
                        pixelOffset: pixelOffset,
                        eyeOffset: eyeOffset,
                        scale: options.scale,
                        image: that._data.basePath + that._data.image + "?datatype=image",
                        color: color,
                        rotation: options.rotation,
                        width: options.width,
                        height: options.height,
                        distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 10000)
                    });

                    if (feature.geometry.coordinates[0] < west) {
                        west = feature.geometry.coordinates[0];
                    }
                    if (feature.geometry.coordinates[0] > east) {
                        east = feature.geometry.coordinates[0];
                    }
                    if (feature.geometry.coordinates[1] < south) {
                        south = feature.geometry.coordinates[1];
                    }
                    if (feature.geometry.coordinates[1] > north) {
                        north = feature.geometry.coordinates[1];
                    }
                    let name = feature.properties[labelField];

                    ctx.font = labelFont;
                    let width = ctx.measureText(name).width + 10;
                    let height = 30;
                    canvastext.width = width;
                    canvastext.height = height;
                    ctx.fillStyle = labelBackgroundColorCanvas;
                    ctx.fillRect(0, 0, canvastext.width, canvastext.height);
                    ctx.fillStyle = labelColorCanvas;
                    ctx.textAlign = 'center';
                    ctx.textBaseline = 'middle';
                    ctx.fillText(name, canvastext.width / 2, canvastext.height / 2);
                    labelCollection.add({
                        position: Cesium.Cartesian3.fromDegrees(
                            feature.geometry.coordinates[0],
                            feature.geometry.coordinates[1],
                            100
                        ),
                        image: ctx.getImageData(0, 0, width, height),
                        pixelOffset: labelPixelOffset,
                        distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 10000)
                    });
                });
                that._rect = new Cesium.Rectangle(Cesium.Math.toRadians(west), Cesium.Math.toRadians(south), Cesium.Math.toRadians(east), Cesium.Math.toRadians(north));

                let collection = new Cesium.PrimitiveCollection();
                collection.add(billboardCollection);
                collection.add(labelCollection);
                that._dataObject = collection;
                that._viewer.scene.primitives.add(collection, that._primitiveIndex);
            }
        );

    }


    close() {
        if (this._dataObject) {
            if (this._viewer.scene.primitives.contains(this._dataObject)) {
                this._viewer.scene.primitives.remove(this._dataObject);
            }
            this._dataObject = null;
        }
    }

    flyTo() {
        if (this._rect !== null) {
            this._viewer.camera.flyTo({
                destination: this._rect
            });
        }
    }
    updateInternalIndex(internalIndex){
        if(this._data.visible)
        {
            this._primitiveIndex = internalIndex.primitiveIndex;
            ++internalIndex.primitiveIndex;
        }
    }
}

export default BillboardData;
